package at.ac.univie.zsu.aguataplan.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import at.ac.univie.zsu.aguataplan.dao.Dao;
import at.ac.univie.zsu.aguataplan.dao.DetectedEventDao;
import at.ac.univie.zsu.aguataplan.domain.DetectedEvent;
import at.ac.univie.zsu.aguataplan.domain.Event;
import at.ac.univie.zsu.aguataplan.domain.EventAttribute;
import at.ac.univie.zsu.aguataplan.domain.EventAttributeValue;
import at.ac.univie.zsu.aguataplan.domain.Video;
import at.ac.univie.zsu.aguataplan.exception.DatabaseException;

/**
 * @author gerry
 * 
 */
@Service
public class DetectedEventManagerBean implements DetectedEventManager {
	private static Logger log = Logger.getLogger(DetectedEventManagerBean.class);

	@Autowired
	private DetectedEventDao detectedEventDao;

	@Autowired
	private Dao dao;

	// ----------------------------------------------------
	// CONSTRUCTOR
	// ----------------------------------------------------

	public DetectedEventManagerBean() {
	}

	// ----------------------------------------------------
	// METHODS
	// ----------------------------------------------------

	@Override
	public void cleanDetectedEvent() throws DatabaseException {
		try {
			detectedEventDao.cleanDetectedEvent();
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entries", e.getMessage());
		}
	}

	// @Override
	// public DetectedEvent addEventToDetectedEvent(Event event, float
	// detPosition, long detTime, double length,
	// Video video) throws DatabaseException {
	// DetectedEvent detectedEvent = new DetectedEvent();
	// detectedEvent.setDetPosition(detPosition);
	// detectedEvent.setDetTime(detTime);
	// detectedEvent.setLength(length);
	// detectedEvent.setRefVideo(video);
	// detectedEvent.setRefEvent(event);
	//
	// DetectedEvent retDetectedEvent = null;
	//
	// try {
	// retDetectedEvent =
	// detectedEventDao.addEventToDetectedEvent(detectedEvent);
	// } catch (Exception e) {
	// log.error(e.getMessage());
	// throw new DatabaseException("could not insert entry", e.getMessage());
	// }
	//
	// return retDetectedEvent;
	// }

	@Override
	public DetectedEvent addEventToDetectedEvent(Event event, float detPosition, long detTime, double length,
			Video video) throws DatabaseException {
		DetectedEvent detectedEvent = new DetectedEvent();
		detectedEvent.setDetPosition(detPosition);
		detectedEvent.setDetTime(detTime);
		detectedEvent.setLength(length);
		detectedEvent.setRefVideo(video);
		detectedEvent.setRefEvent(event);

		return detectedEvent;
	}

	// @Override
	// public DetectedEvent addEventAttributeToDetectedEvent(DetectedEvent
	// detectedEvent, EventAttribute eventAttribute)
	// throws DatabaseException {
	// detectedEvent.setRefEventAttribute(eventAttribute);
	//
	// DetectedEvent retDetectedEvent = null;
	//
	// try {
	// retDetectedEvent = detectedEventDao.mergeDetectedEvent(detectedEvent);
	// } catch (Exception e) {
	// log.error(e.getMessage());
	// throw new DatabaseException("could not save entry", e.getMessage());
	// }
	//
	// return retDetectedEvent;
	// }

	@Override
	public DetectedEvent addEventAttributeToDetectedEvent(DetectedEvent detectedEvent, EventAttribute eventAttribute)
			throws DatabaseException {
		detectedEvent.setRefEventAttribute(eventAttribute);

		return detectedEvent;
	}

	// @Override
	// public DetectedEvent addEventAttributeValueToDetectedEvent(DetectedEvent
	// detectedEvent,
	// EventAttributeValue eventAttributeValue) throws DatabaseException {
	// detectedEvent.setRefEventAttributeValue(eventAttributeValue);
	//
	// DetectedEvent retDetectedEvent = null;
	//
	// try {
	// retDetectedEvent = detectedEventDao.mergeDetectedEvent(detectedEvent);
	// } catch (Exception e) {
	// log.error(e.getMessage());
	// throw new DatabaseException("could not save entry", e.getMessage());
	// }
	//
	// return retDetectedEvent;
	// }

	@Override
	public DetectedEvent addEventAttributeValueToDetectedEvent(DetectedEvent detectedEvent,
			EventAttributeValue eventAttributeValue) throws DatabaseException {
		detectedEvent.setRefEventAttributeValue(eventAttributeValue);

		DetectedEvent retDetectedEvent = null;

		try {
			retDetectedEvent = detectedEventDao.addEventToDetectedEvent(detectedEvent);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not save entry", e.getMessage());
		}

		return retDetectedEvent;
	}

	@Override
	public List<DetectedEvent> getDetectedEventByVideoList(Video video) throws DatabaseException {
		List<DetectedEvent> detectedEvents = new ArrayList<DetectedEvent>();

		try {
			detectedEvents = detectedEventDao.getDetectedEventByVideo(video);
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}
		return detectedEvents;
	}

	@Override
	public void deleteDetectedEvent(DetectedEvent detectedEvent) throws DatabaseException {
		try {
			detectedEventDao.deleteDetectedEvent(detectedEvent.getId());
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not delete entry", e.getMessage());
		}

	}

	@Override
	public List<DetectedEvent> getDetectedEventByEvent(Event event, Video video) throws DatabaseException {
		List<DetectedEvent> detectedEvents = new ArrayList<DetectedEvent>();

		try {
			if (event == null) {
				detectedEvents = detectedEventDao.getDetectedEventByVideo(video);
			} else {
				detectedEvents = detectedEventDao.getDetectedEventByEventAndVideo(event, video);
			}
		} catch (Exception e) {
			log.error(e.getMessage());
			throw new DatabaseException("could not get entries", e.getMessage());
		}

		return detectedEvents;
	}

	// ----------------------------------------------------
	// GETTER & SETTER
	// ----------------------------------------------------

	public Dao getDao() {
		return dao;
	}

	public void setDao(Dao dao) {
		this.dao = dao;
	}

	public DetectedEventDao getDetectedEventDao() {
		return detectedEventDao;
	}

	public void setDetectedEventDao(DetectedEventDao detectedEventDao) {
		this.detectedEventDao = detectedEventDao;
	}

}
